package com.hiwan.unions.activity.system;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.activity.loginRegister.LoginUserActivity;
import com.hiwan.unions.activity.main.MainActivity;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.exception.ApiException;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.EditUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.ToastUtils;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONObject;

/**
 * Created by llg on 2017/7/8.
 */
public class UpdatePassword extends RootActivity implements HttpListener<JSONObject> {
    private EditText edit_password2;
    private EditText edit_password;
    private ImageView imageView1, imageView2;
    private String password1, password2;
    private Request<JSONObject> request;
    boolean eyeOpen1 = false;
    boolean eyeOpen2 = false;

    @Override
    protected int getLayout() {
        return R.layout.activity__update_passwod;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        edit_password2 = findView(R.id.edit_password2);
        edit_password = findView(R.id.edit_password);
        setTitleTxt("修改密码");

        imageView1 = findView(R.id.image1);

        imageView2 = findView(R.id.image2);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eyeOpen1 = EditUtils.YjImage(imageView1, edit_password2, eyeOpen1);
                edit_password2.requestFocus();
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eyeOpen2 = EditUtils.YjImage(imageView2, edit_password, eyeOpen2);
                edit_password.requestFocus();
            }
        });
    }

    @Override
    protected void IntentConfirm() {
        super.IntentConfirm();
        try {
            password1 = CheckUtils.checkUpdatePaaword(edit_password2);
            password2 = CheckUtils.checkUpdatePaaword2(edit_password);
        } catch (ApiException e) {
            ToastUtils.showToast(this, e.getMessage());
            return;
        }
        request = NoHttp.createJsonObjectRequest(ConstsNet.updatePassword, RequestMethod.POST);
        request.add("oldPassword", password1);
        request.add("password", password2);
        CallServer.getRequestInstance().addToken(this, 1, request, this, true, userInfo);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        JSONObject jsonObject = response.get();
        ToastUtils.showToast(this, "修改成功");
        SharedPreferences.Editor editor = userInfo.edit();
        editor.clear();
        editor.putBoolean(ConstantsValues.IS_HASLOGIN, false);
        editor.commit();

        Intent mainIntent = new Intent(this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Intent detailIntent = new Intent(this, LoginUserActivity.class);
        Intent[] intents = {mainIntent, detailIntent};
        startActivities(intents);
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}